﻿using System.Collections.Generic;
using System.Xml.Linq;
using QASPRibbon.Commands;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Represents a TextBox control in the SharePoint Server Ribbon.
    /// </summary>
    public class RibbonTextBox : RibbonVisualControl
    {
        #region JavaScript

        /// <summary>
        /// Client-side code that executes when a TextBox is activated.
        /// </summary>
        public string OnClientActivate { get; set; }

        /// <summary>
        /// Client-side code that executes when a user leaves an input field.
        /// </summary>
        public string OnClientBlur { get; set; }

        /// <summary>
        /// Client-side code that executes when a TextBox's enable/disable state is checked.
        /// </summary>
        public string OnClientStateCheck { get; set; }

        #endregion

        #region Properties

        ///<summary>
        /// A value that indicates whether Input Method Editor (IME) is activated.
        /// The default is False.
        ///</summary>
        public bool ImeEnabled { get; set; }

        ///<summary>
        /// A value that indicates whether TextBox controls renders as simple label (read-only mode).
        /// The default is False.
        ///</summary>
        public bool ShowAsLabel { get; set; }

        ///<summary>
        /// A value that indicates whether Input Method Editor (IME) is activated.
        /// The default is 100.
        ///</summary>
        public int MaxLength
        {
            get { return _maxLength; }
            set { _maxLength = value; }
        }

        ///<summary>
        /// A value that indicates whether Input Method Editor (IME) is activated.
        /// The default is 100.
        ///</summary>
        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        #endregion

        #region Internal

        internal override string XmlTag
        {
            get { return Schema.TextBox; }
        }

        internal override IEnumerable<RibbonCommand> ClientCommands
        {
            get
            {
                var res = new List<RibbonCommand>
                              {
                                  new RibbonCommand(FullId, Schema.CheckState,
                                                    string.Format(
                                                        "{0}; {1}; return {2}",
                                                        OnClientActivate ?? string.Empty, OnClientStateCheck,
                                                        Enabled ? "true" : "false")),
                                  new RibbonCommand(FullId, Schema.Command, string.Format(
                                      "var value = properties.Value; {0};",
                                      OnClientBlur))
                              };
                return res;
            }
        }

        internal override XElement AsXElement()
        {
            var res = base.AsXElement();
            res.Add(new XAttribute(Schema.LabelText, Title));
            if (!string.IsNullOrEmpty(TemplateAlias))
                res.Add(new XAttribute(Schema.TemplateAlias, TemplateAlias));
            res.Add(new XAttribute(Schema.ImeEnabled, ImeEnabled ? "true" : "false"));
            res.Add(new XAttribute(Schema.ShowAsLabel, ShowAsLabel ? "true" : "false"));
            res.Add(new XAttribute(Schema.MaxLength, MaxLength));
            res.Add(new XAttribute(Schema.Width, Width));
            res.Add(new XAttribute(Schema.Command, FullId + Schema.Command));
            res.Add(new XAttribute(Schema.QueryCommand, FullId + Schema.QueryCommand));
            return res;
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates and initializes a new instance of the RibbonTextBox.
        /// </summary>
        public RibbonTextBox() { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonTextBox.
        /// </summary>
        /// <param name="id">Identifier</param>
        public RibbonTextBox(string id) : base(id) { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonTextBox.
        /// </summary>
        /// <param name="id">Identifier</param>
        /// <param name="title">Title</param>
        public RibbonTextBox(string id, string title) : base(id, title) { }

        #endregion

        #region Private

        private int _maxLength = 200;
        private int _width = 100;

        #endregion
    }
}
